package ess.hardware;

public class Motor implements Runnable{
	
	int motorState = MOTOR_STATE_STOPPED;
	int operationalDirection = MOTOR_OPERATION_NONE;
	long sleepTime = 0;
	float degree = 0;
	
	MotoredComponent motoredComp;
	
	public static int MOTOR_STATE_STOPPED = 0;
	public static int MOTOR_STATE_MOVING = 1;
	
	public static int MOTOR_OPERATION_NONE = 0;
	public static int MOTOR_OPERATION_UP = 1;
	public static int MOTOR_OPERATION_DOWN = 2;
	public static int MOTOR_OPERATION_OPEN = 3;
	public static int MOTOR_OPERATION_CLOSE = 4;
	
	
	public Motor(MotoredComponent obj, long sleepTime, float degree)
	{
		motoredComp = obj;
		this.sleepTime = sleepTime;
		this.degree = degree;
		new Thread(this).start();
	}
	
	public void run() {
		for(;;)
		{
			while(motorState == MOTOR_STATE_STOPPED)
			{
				synchronized(this){
					try {
						this.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			
			try {
				Thread.sleep(sleepTime);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			if(motorState != MOTOR_STATE_STOPPED){
				motoredComp.moveMotoredComponent(operationalDirection, degree);
			}
		}
		
	}
	
	public boolean move(int direction)
	{
		motorState = MOTOR_STATE_MOVING;
		operationalDirection = direction;
		synchronized(this){
			this.notify();
		}
		return true;
	}
	
	public boolean stop()
	{
		motorState = MOTOR_STATE_STOPPED;
		return true;
	}

}
